package com.wgy.app;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;


import com.wgy.cn.FileTools;

public class FileDemo {

	public static void main(String[] args) throws IOException {

		// 创建properties对象
		Properties properties = new Properties();
		//创建类加载器
		//ClassLoader classLoader = FileDemo.class.getClassLoader();
		// 用相对路径创建输入流
		InputStream input =new BufferedInputStream(Files.newInputStream(Paths.get("config/app.properties")));
		properties.load(input);
		//PrintStream old = System.out;
		File filePath = new File(FileDemo.decode(properties.getProperty("filePath")));
		// 日志路径
		String logPath = FileDemo.decode(properties.getProperty("logPath"));
		//数据保留天数
		String days = properties.getProperty("logDays");
		//System.out.println(properties.getProperty("logPath"));

		//创建日志文件对象
		File log = new File(logPath);
		// 如果路径不存在则进行创建
		if (!log.exists()) {
			boolean flag = log.mkdirs();
			if (flag){
				System.out.println(logPath+" build success");
			}
		}
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		logPath = logPath + dateFormat.format(date) + ".log";

		//创建日志输出流
		PrintStream ps = new PrintStream(Files.newOutputStream(Paths.get(logPath)));

		//删除相关文件
		FileTools.delete(filePath,days,ps);

		//设置输出流的输出位置
		System.setOut(FileTools.old);
		System.out.println("数据已清理完成，请查看日志，路径："+'"'+logPath+'"');
	}


	//解决utf-8中文乱码问题
	public static String decode(String str) {
		return  new String(str.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
	}

}
